'use strict'

class collectmusic {
  constructor() {
    this.param = '/'
  }

  async get(c) {
    let user_id = c.box.user.openid,
      ret = await c.service.psql.query(`select * from collectmusic where collectmusic_user_id like '${user_id}'`),
      resultArr = ret.rows,
      respondArr = []
    for (let i = 0; i < resultArr.length; i++) {
      var temp = await c.service.psql.query(`select * from music where music_id like '${resultArr[i].collectmusic_id}' order by music_id asc`)
      respondArr.push(temp.rows[0])
    }
    c.status(200).send(respondArr)
  }

  async post(c) {
    console.log(c.body.id)
    var user_id = c.box.user.openid,
      obj = c.body,
      id = obj.id
    let ret2 = await c.service.psql.query(`select * from music where music_id like '${id}'`)
    if (!ret2.rows[0]) {
      c.status(400).send('music_id 有误 :)')
      return
    }
    let ret = await c.service.psql.query(`select * from collectmusic where collectmusic_id like '${id}' and collectmusic_user_id like '${user_id}'`)
    console.log(ret.rows)
    if (ret.rows[0]) {
      ret = await c.service.psql.query(`delete from collectmusic where collectmusic_id like '${id}' and collectmusic_user_id like '${user_id}'`)
      c.status(200).send('unlike success')
    } else {
      ret = await c.service.psql.query(`insert into collectmusic values('${id}', '${user_id}')`)
      c.status(200).send('like success')
    }
  }
}

module.exports = collectmusic